package com.lm.activiti.boot.annotation.test;

import com.google.common.collect.Lists;
import com.lm.activiti.boot.dto.Animal;
import com.lm.activiti.boot.dto.Food;
import org.apache.poi.ss.formula.functions.T;
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;

import javax.validation.*;
import java.util.List;
import java.util.Set;

/**
 * @author lm_jcf
 * @date 2020/11/3 15:44
 */
public class BeanValidation {

    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.setFood(new Food());
        animal.setFoodList(Lists.newArrayList(new Food()));

        validateConstraint(animal);

        /*HibernateValidatorConfiguration configure = Validation.byProvider(HibernateValidator.class).configure();

        ValidatorFactory validatorFactory = configure.failFast(false).buildValidatorFactory();

        // 根据 validatorFactory 拿到一个 Validator

        Validator validator = validatorFactory.getValidator();

        Set<ConstraintViolation<Animal>> validate = validator.validate(animal);

        validate.stream().map(result -> result.getPropertyPath() + " " + result.getMessage() + ": " + result.getInvalidValue()).forEach(System.out::println);

*/
        /*for (Food food : animal.getFoodList()) {

            Set<ConstraintViolation<Food>> validate_child = validator.validate(food);

            validate_child.stream().map(result -> result.getPropertyPath() + " " + result.getMessage() + ": " + result.getInvalidValue()).forEach(System.out::println);
        }*/

    }

    private static void validateConstraint(Object object){
        HibernateValidatorConfiguration configure = Validation.byProvider(HibernateValidator.class).configure();

        ValidatorFactory validatorFactory = configure.failFast(false).buildValidatorFactory();

        // 根据 validatorFactory 拿到一个 Validator

        Validator validator = validatorFactory.getValidator();

        Set<ConstraintViolation<Object>> validate = validator.validate(object);

        validate.stream().map(result -> result.getPropertyPath() + " " + result.getMessage() + ": " + result.getInvalidValue()).forEach(System.out::println);
    }
}
